package com.cw.base.common.model.util;

import java.util.Date;

import com.cw.base.common.model.dto.base.BaseRequest;
import com.cw.base.common.model.dto.base.BaseUserRequest;
import com.cw.base.common.model.enums.EnumContext;
import com.cw.base.common.model.enums.EnumModuleCode;
import com.cw.lang.common.utils.context.ThreadContextStoreUtil;

/**
 * @author chenw
 * @version $$Id: AppParamUtil.java, v 0.1 2018/9/19 下午2:11 chenw Exp $$
 */
public final class AppParamUtil {

    /**
     * 设置线程上下文
     *
     * @param baseRequest 上线文内容
     */
    public static void setRequest(BaseRequest baseRequest) {
        if (baseRequest == null) {
            return;
        }
        // 设置线程上下文
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.CHANNEL_NO.getCode(), baseRequest.getChannelNo());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.SERIAL_NO.getCode(), baseRequest.getSerialNo());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.TRANS_SERIAL_NO.getCode(), baseRequest.getIdemSerialNo());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.TRANS_DATETIME.getCode(), baseRequest.getTransDateTime());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.SYSTEM_ID.getCode(), null != baseRequest.getAsk() ? baseRequest.getAsk()
                .getCode() : EnumModuleCode.UNKNOWN.getCode());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.BASE_REQUEST.getCode(), baseRequest);
    }

    /**
     * 设置userRequest
     *
     * @param userRequest userRequest
     */
    public static void setUserRequest(BaseUserRequest userRequest) {
        if (userRequest == null) {
            return;
        }
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.OPERATOR_ID.getCode(), userRequest.getOperatorId());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.OPERATOR_NAME.getCode(), userRequest.getOperatorName());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.ORGAN_ID.getCode(), userRequest.getOrganId());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.ORGAN_NAME.getCode(), userRequest.getOrganName());
        ThreadContextStoreUtil.getInstance()
            .set(EnumContext.BASE_USER_REQUEST.getCode(), userRequest);
    }

    /**
     * 清理线程上下文的BaseRequest
     */
    public static void removeRequest() {
        // 清理线程上下文
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.CHANNEL_NO.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.SERIAL_NO.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.TRANS_SERIAL_NO.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.TRANS_DATETIME.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.SYSTEM_ID.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.BASE_REQUEST.getCode());
    }

    /**
     * 清理线程上下文中的BaseUserRequest
     */
    public static void removeUserRequest() {
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.OPERATOR_ID.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.OPERATOR_NAME.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.ORGAN_ID.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.ORGAN_NAME.getCode());
        ThreadContextStoreUtil.getInstance()
            .removeKey(EnumContext.BASE_USER_REQUEST.getCode());
    }

    /**
     * 请求系统编号
     *
     * @return 系统编号
     */
    public static String getReqSystemId() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.SYSTEM_ID.getCode());
    }

    /**
     * 获得渠道号
     *
     * @return 渠道号
     */
    public static String getChannelNo() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.CHANNEL_NO.getCode());
    }

    /**
     * 获得流水号
     *
     * @return 流水号
     */
    public static String getSerialNo() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.SERIAL_NO.getCode());
    }

    /**
     * 获得交易时间
     *
     * @return 交易时间
     */
    public static Date getTransDatetime() {
        return (Date) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.TRANS_DATETIME.getCode());
    }

    /**
     * 获取报文头
     *
     * @return 报文头
     */
    public static BaseRequest getBaseRequest() {
        return (BaseRequest) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.BASE_REQUEST.getCode());
    }

    /**
     * 获取用户请求对象
     *
     * @return 用户请求对象
     */
    public static BaseUserRequest getBaseUserRequest() {
        return (BaseUserRequest) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.BASE_USER_REQUEST.getCode());
    }

    /**
     * 获取交易流水号
     *
     * @return 交易流水号
     */
    public static String getTransSerialNo() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.TRANS_SERIAL_NO.getCode());
    }

    /**
     * 获取操作人id
     *
     * @return 操作人id
     */
    public static String getOperatorId() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.OPERATOR_ID.getCode());
    }

    /**
     * 获取操作人姓名
     *
     * @return 操作人姓名
     */
    public static String getOperatorName() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.OPERATOR_NAME.getCode());
    }

    /**
     * 获取机构id
     *
     * @return 机构id
     */
    public static String getOrganId() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.ORGAN_ID.getCode());
    }

    /**
     * 获取机构名称
     *
     * @return 机构名称
     */
    public static String getOrganName() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(EnumContext.ORGAN_NAME.getCode());
    }

}
